package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.Power;
import com.colorlight.cloud.complete.entity.po.PrcGroup;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 获取发送卡电源状态
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetSenderPowerStatus implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 发送卡屏幕信息
         */
        private List<PrcGroup> prcGroup;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 屏幕状态
         */
        private List<Power> power;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.GetSenderPowerStatus.InputMsg.Builder inputMsg =
                CompleteInstruction.GetSenderPowerStatus.InputMsg.newBuilder();
        GetSenderPowerStatus.InputMsg thisInput = this.getInputMsg();
        inputMsg.addAllPrcGroup(thisInput.getPrcGroup().stream().map(PrcGroup::toProto).collect(Collectors.toList()));
        final CompleteInstruction.GetSenderPowerStatus.Builder builder =
                CompleteInstruction.GetSenderPowerStatus.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public GetSenderPowerStatus byteConvertObject(ByteString bytes) {
        CompleteInstruction.GetSenderPowerStatus getSenderPowerStatus = null;
        try {
            getSenderPowerStatus = CompleteInstruction.GetSenderPowerStatus.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(getSenderPowerStatus)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        GetSenderPowerStatus.OutputMsg outputMsg = new GetSenderPowerStatus.OutputMsg();
        CompleteInstruction.GetSenderPowerStatus.OutputMsg output = getSenderPowerStatus.getOutput();
        List<Power> collect = output.getPowerList().stream().map(e -> new Power().formProto(e)).collect(Collectors.toList());
        outputMsg.setPower(collect);
        this.setOutputMsg(outputMsg);
        return this;
    }

}

